package com.codewithmosh.store.controllers;

import com.codewithmosh.store.dto.CheckoutRequest;
import com.codewithmosh.store.dto.CheckoutResponse;
import com.codewithmosh.store.dto.ErrorVo;
import com.codewithmosh.store.exceptions.CartEmptyException;
import com.codewithmosh.store.exceptions.CartNotFoundException;
import com.codewithmosh.store.services.CheckoutService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkout")
@RequiredArgsConstructor
public class CheckoutController {
    private final CheckoutService checkoutService;

    @PostMapping
    public ResponseEntity<CheckoutResponse> checkout(
            @Valid @RequestBody CheckoutRequest request
    ) {
        CheckoutResponse response = checkoutService.checkout(request);

        return ResponseEntity.ok(response);
    }

    @ExceptionHandler({CartNotFoundException.class, CartEmptyException.class})
    public ResponseEntity<ErrorVo> handleCartEmptyException(Exception e) {
        return ResponseEntity.badRequest().body(new ErrorVo(e.getMessage()));
    }
}
